package threads;

import java.util.*;

public class MyThread {

	public static void main(String[] args) {
		// Thread1 r1 = new Thread1();
		// r1.run();
		// Thread2 r2 = new Thread2();
		// r2.run();

		// Thread t1 = new Thread(r1);
		// t1.run();
		// Thread t2 = new Thread(r1);
		// t2.run();
		final Thread3 r3 = new Thread3();
		new Thread(new Runnable() {
			@Override
			public void run() {
				Calendar cal = Calendar.getInstance();
				int minute = cal.get(Calendar.MINUTE);
				Calendar com = Calendar.getInstance();
				while (com.get(Calendar.MINUTE) == minute) {
					com = Calendar.getInstance();
				}
				r3.stop();
			}
		}).start();
		r3.run();
	}

}

class Thread1 implements Runnable {

	@Override
	public void run() {
		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("Hello, I am in Thread1.");
		System.out.println(new Date());
	}

}

class Thread2 implements Runnable {

	@Override
	public void run() {
		System.out.println("Hey, I am in Thread2.");
		System.out.println(new Date());
	}

}

class Thread3 implements Runnable {

	private boolean stop = false;

	@Override
	public void run() {
		while (!stop) {
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("I am running.");
		}
	}

	public void stop() {
		this.stop = true;
		System.out.println("I am stopped.");
	}
}
